<% layout('/layouts/default.html', {title: '文件分发', libs: ['validate','zTree']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header">

            <div class="box-title">
                <i class="fa icon-notebook"></i> ${text('文件基本信息')}
            </div>
		</div>
		<#form:form id="inputForm" model="${docRecv}" action="${ctx}/sd/docRecv/distribute" method="post" class="form-horizontal">
			<div class="box-body">

				<div class="row mt20" >
					<div class="col-xs-4">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								${text('来文标题')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<input readonly class="form-control width-260" value="${docRecv.docTitle}" />
							</div>
						</div>
					</div>
					<div class="col-xs-4">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								${text('来文文号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<input readonly class="form-control width-260" value="${docRecv.docFullNum}" />
							</div>
						</div>
					</div>
					<div class="col-xs-4">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								${text('文件密级')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<input id="security" readonly="true"  class="form-control width-120" value=""/>
							</div>
						</div>
					</div>
				</div>
				<#form:hidden path="id"/>
                <hr/>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span>${text('规定截止领文日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input autocomplete="off" id="d1" path="beforeTakeDate" defaultValue="${beforeTakeDate}"  maxlength="20" class="form-control laydate required"
									dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span>${text('规定截止还文日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input autocomplete="off" id="d2" path="beforeReturnDate" defaultValue="${beforeReturnDate}"   maxlength="20" class="form-control laydate required"
								dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
							</div>
						</div>
					</div>
				</div>
				<div class="form-unit">${text('单位选择')}</div>

				<div class="row">
					<div class="col-xs-10">
						<div id="dataScopeTrees"></div>
						<script id="dataScopeTpl" type="text/template">
							<div class="pull-left" style="padding:0 15px;min-width:300px;">
								<div class="box box-solid" style="background:#FAFAFA">
									<div class="box-header">
										<div class="box-title icheck">
											<label><input type="checkbox" id="checkall_{{d.key}}"
														  class="checkall"/> {{d.label}}</label>
										</div>
										<div class="box-tools pull-right" style="top:8px;">
											<a class="btn btn-box-tool" id="expand_{{d.key}}"
											   value="dataScopeTree_{{d.key}}" >${text('展开')}</a>/<a
												class="btn btn-box-tool" id="collapse_{{d.key}}"
												value="dataScopeTree_{{d.key}}" >${text('折叠')}</a>
										</div>
									</div>
									<div class="box-body">
										<div id="dataScopeTree_{{d.key}}" class="ztree"></div>
									</div>
								</div>
							</div>
						</script>
						<#form:checkbox path="isPrint" label="打印二维码" checked="checked" class="form-control"/>
					</div>
					<div class="col-xs-2">
						<div></div>
					</div>
					<#form:hidden name="userDataScopeListJson"/>
				</div>
			</div>
			<div class="box-footer">
				<div class="row hidden">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('sd:docRecv:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('分 发')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
    $("#security").val(js.getDictLabel(${@DictUtils.getDictListJson('recv_sec_lvl')}, ${docRecv.secLvl}, '${text("未知")}', true));
$("#inputForm").validate({
	submitHandler: function(form){

		var dataScopeData = [];
		$.each(dataScopeTrees, function(key, dataScopeTree){
			var treeNodes = dataScopeTree.getCheckedNodes(true);
			for(var i=0; i<treeNodes.length; i++) {
				dataScopeData.push({
					ctrlType: key, ctrlData: treeNodes[i].id
				});
			}
		});
		$("#userDataScopeListJson").val(JSON.stringify(dataScopeData));

		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
			page();
		}, "json");
    }
});


//加载数据权限树结构
var setting = {
			check:{enable:true,nocheckInherit:true},
			view:{selectedMulti:false,nameIsHTML: true},
			data:{simpleData:{enable:true},key:{title:"title"}},
			callback:{
				beforeClick: function (treeId, treeNode, clickFlag) {
					var tree = $.fn.zTree.getZTreeObj(treeId);
					tree.checkNode(treeNode, !treeNode.checked, true, true);
					return false;
				},
				onCheck: function (event, treeId, treeNode){
					var tid = treeNode.tId;
					if(!treeNode.checked){
						$(".checkall[value="+treeId+"]").each(function(){
							if(this.checked){
								$(this).removeAttr("checked").iCheck('update');
							}
							return false;
						});
					}
				}
			}
		},
		moduleCodes = '${toJson(@ModuleUtils.getEnableModuleCodes())}';
		dataScopes = ${@Global.getConfig('user.sendDataScopes','[]')},
		dataScopeTrees = {}; // 用sysCode分类存储所有菜单树

/**
 * chkboxType: {Y: "ps", N: "ps"}
 ctrlDataUrl: "/sys/office/treeData"
 ctrlName: "接收单位"
 ctrlName_en: "Office"
 ctrlPermi: "0"
 ctrlType: "Office"
 expandLevel: -1
 moduleCode: "core"
 remarks: ""
 */
for (var i = 0; i < dataScopes.length; i++) {
	var dataScope = dataScopes[i];
	$('#dataScopeTrees').append(js.template('dataScopeTpl', {
		key: dataScope.ctrlType,
		label: dataScope.ctrlName_${lang()} || dataScope.ctrlName
	}
		));
	var ctrlDataUrl = dataScope.ctrlDataUrl+'&docId='+'${docRecv.id}' || '';
	$.ajax({
		type: 'POST',
		url: "${ctx}" + ctrlDataUrl + (ctrlDataUrl.indexOf("?")!=-1?'&':'?') + "___t=" + new Date().getTime(),
		data: {ctrlPermi: '${@Global.getConfig("user.adminCtrlPermi", "2")}'},
		dataType: 'json',
		async: false,
		error: function(data){
			js.showErrorMessage(data.responseText);``
		},
		success: function(data, status, xhr){
			// 初始化树结构
			var tree = $.fn.zTree.init($("#dataScopeTree_"+dataScope.ctrlType), setting, data);

			tree.setting.check.chkboxType = dataScope.chkboxType;
			// 默认展开节点（如果级别设置为-1，则：如果有1个根节点，则展开一级节点，否则不展开）
			$.fn.zTree.expandNodeByLevel(tree, dataScope.expandLevel);
			// 树结构：全选、取消全选
			$('#checkall_'+dataScope.ctrlType).iCheck({
				checkboxClass:'icheckbox_minimal-grey'
			}).on('ifChecked ifUnchecked', function(){
				var ctrlType = $(this).attr('ctrlType');
				if(this.checked){
					dataScopeTrees[ctrlType].checkAllNodes(true);
				}else{
					dataScopeTrees[ctrlType].checkAllNodes(false);
				}
			}).attr("ctrlType", dataScope.ctrlType);
			// 展开和折叠按钮绑定
			$('#expand_'+dataScope.ctrlType).click(function(){
				var ctrlType = $(this).attr('ctrlType');
				dataScopeTrees[ctrlType].expandAll(true);
			}).attr("ctrlType", dataScope.ctrlType);
			$('#collapse_'+dataScope.ctrlType).click(function(){
				var ctrlType = $(this).attr('ctrlType');
				dataScopeTrees[ctrlType].expandAll(false);
			}).attr("ctrlType", dataScope.ctrlType);
			// 将树对象存储到全局数组里
			dataScopeTrees[dataScope.ctrlType] = tree;


		}
	});
}
// 默认选择节点

</script>